import store from '@/store';
import { decryptByDES } from "@/utils/utils";


export default {
    install(app) {
        app.directive("permission", {
            mounted(el, binding) {
                hasPermission(binding.value, el);
            },
        })
    }
}

function hasPermission(value, el = false) {
    if (!Array.isArray(value)) {
        throw new Error("需要配置权限，比如：v-permission=['201']")
    }


    const permissionList = store.getters["login/permissons"];
    var permissions = permissionList.map(p => decryptByDES(p.code) * 1);
    const hashAuth = value.findIndex(v => permissions.includes(v)) != -1;
    if (el && !hashAuth) {
        el.parentNode && el.parentNode.removeChild(el);
    }

    return hashAuth;
}